package com.hz.foodstalls.shiro.freemarker;

import freemarker.core.Environment;
import freemarker.log.Logger;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import org.apache.shiro.session.Session;

import java.io.IOException;
import java.util.Map;

/**
 * Created by foodstalls on 2016/2/18.
 */
public class LoginUsernameTag extends SecureTag {
    static final Logger log = Logger.getLogger("LoginUsernameTag");

    public static final String DEFAULT_USERNAME_PARAM = "username";

    @Override
    public void render(Environment env, Map params, TemplateDirectiveBody body) throws IOException, TemplateException {
        String result = null;
        Session session = getSubject().getSession();
        if (session != null && session.getAttribute(DEFAULT_USERNAME_PARAM) != null) {
            if (log.isDebugEnabled()) {
                log.debug("tempUser is exsit.");
            }

            String username = session.getAttribute(DEFAULT_USERNAME_PARAM).toString();
            if (username != null) {
                result = username;
            }
        } else {
            if (log.isDebugEnabled()) {
                log.debug("Attr is not exsit.");
            }
        }

        if (result != null) {
            try {
                env.getOut().write(result);
            } catch (IOException ex) {
                throw new TemplateException("Error writing [" + result + "] to Freemarker.", ex, env);
            }
        } else {
            renderBody(env, body);
        }
    }
}
